-- compound
-- created by zhuangsl
-- 冈布奥事件：合成物品

-- 合成物品
EventMgr.register("compound", event.DO_COMPOUND, function(para)
    local ruleId = para.ruleId;

    -- 合成类型
    local compoundType = CompoundM.query(ruleId, "type");

    -- 如果需要增加进度
    if "pearl" == compoundType then
        local key = string.format("%s_%s", "compound", compoundType);
        SlimeEventM.addProgress(key);
    end

    local arr = SlimeEventM.getIdsByRule("compound");
    local dbase;
    for index = 1, #arr do
        -- 尝试触发对应类型的事件
        dbase = SlimeEventM.query(arr[index], "dbase");
        if dbase["compound_rule"] == compoundType then
            SlimeEventM.triggerEvent(arr[index]);
        end
    end
end);

return {
    checkEvent = function(eventId, para)
        local dbase = SlimeEventM.query(eventId, "dbase") or {};

        if dbase["slime"] and PetM.getMyPet(dbase["slime"]) then
            -- 奖励冈布奥的，不能已经有冈布奥
            return false;
        end

        if dbase["slime_scrap"] and not PetM.getMyPet(dbase["slime_scrap"]) then
            -- 奖励冈布奥碎片的，需要先有冈布奥
            return false;
        end

        -- 需要根据进度判断是否触发事件的
        local compoundRule = dbase["compound_rule"];
        if "pearl" == compoundRule then
            local amount = SlimeEventM.query(eventId, "condition");
            local key = string.format("%s_%s", "compound", compoundRule);
            local progress = SlimeEventM.getProgress(key);

            -- 奖励冈布奥的，进度达到要求量时触发
            if dbase["slime"] and progress ~= amount then
                return false;
            end

            -- 奖励碎片的，进度为要求量的多倍时触发
            if dbase["slime_scrap"] then
                if progress % amount ~= 0 or progress <= amount then
                    return false;
                end
            end
        end

        return true;
    end,
};
